package com.benefm.multipar.pad.view;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.os.Build;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.benefm.multipar.pad.R;

import java.util.ArrayList;

public class Ecg12View_DS extends SurfaceView implements SurfaceHolder.Callback {

    protected Paint mPath;
    protected int mWidth, mHeight;
    protected float scale;
    //背景画笔
    protected Paint mPaint;
    //画背景标签
    protected Paint mTagPaint;
    //画字体背景
    protected Paint mTextPaint;
    //网格颜色
    protected int mGridColor = Color.parseColor("#313131");
    //小网格颜色
    protected int mSGridColor = Color.parseColor("#00000000");
    protected int mTagColor = Color.parseColor("#999999");
    //波形颜色
    protected int mWaveColor = Color.parseColor("#5886c0");
    //背景颜色
    protected int mBackgroundColor = Color.parseColor("#1a2028");

    //网格宽度
    protected float mXGridWidth;//固定为5毫米(X轴5毫米距离像素点个数)
    protected float mYGridWidth;//固定为5毫米(Y轴5毫米距离像素点个数)
    //小网格的宽度
    protected int mSGridWidth = 10;
    private ArrayList<Float> data0 = new ArrayList<>();
    private ArrayList<Float> data1 = new ArrayList<>();
    private ArrayList<Float> data2 = new ArrayList<>();
    private ArrayList<Float> data3 = new ArrayList<>();
    private ArrayList<Float> data4 = new ArrayList<>();
    private ArrayList<Float> data5 = new ArrayList<>();
    private ArrayList<Float> data6 = new ArrayList<>();
    private ArrayList<Float> data7 = new ArrayList<>();
    private ArrayList<Float> data8 = new ArrayList<>();
    private ArrayList<Float> data9 = new ArrayList<>();
    private ArrayList<Float> data10 = new ArrayList<>();
    private ArrayList<Float> data11 = new ArrayList<>();
    int index0 = 0;//最近的数据在数组中的索引
    int index1 = 0;//最近的数据在数组中的索引
    int index2 = 0;//最近的数据在数组中的索引
    int index3 = 0;//最近的数据在数组中的索引
    int index4 = 0;//最近的数据在数组中的索引
    int index5 = 0;//最近的数据在数组中的索引
    int index6 = 0;//最近的数据在数组中的索引
    int index7 = 0;//最近的数据在数组中的索引
    int index8 = 0;//最近的数据在数组中的索引
    int index9 = 0;//最近的数据在数组中的索引
    int index10 = 0;//最近的数据在数组中的索引
    int index11 = 0;//最近的数据在数组中的索引
    int maxLines;//一屏幕最多的线段数量
    float xaxis;//x轴单位长度
    float yaxis;//y轴单位长度
    float x = 2f;//x轴单位长度系数
    float y = 1f;//y轴单位长度系数
    int waveSize = 10;//ecg波个数
    int waveHeight;//单个波的高度
    int waveMargin;//波的间隔
    int waveMargin1;//波的间隔
    private Context context;
    // SurfaceHolder
    private SurfaceHolder mHolder;
    // 用于绘图的Canvas
    private Canvas mCanvas;
    // 子线程标志位
    public boolean mIsDrawing = false;
    private boolean mIsDestroyed = false;
    private float xPixelPerMm;//x轴每毫米像素点个数
    private float xdpi;//x轴横向屏幕像素密度
    private float xSpeed = 25f;//走速默认25
    private float sampleRate = 250;//采样率
    private float ydpi;//y轴纵向向屏幕像素密度
    private float yPixelPerMm;//y轴每毫米像素点个数
    private float range;//每毫伏mv幅度(单位像素)
    private float enhance = 10;//y轴增益默认10
    private float zoom = 4.900000f / 1000f;//y轴放大倍数 用来和硬件1mv波形比例对应，所有的y点都需要乘这个比例0.0049
    private float mv;//毫伏
    private int transparentPoints;//透明点个数
    private boolean work = true;
    public String[] labels;

    public Ecg12View_DS(Context context) {
        super(context);
        this.context = context;
        initView();

    }

    public Ecg12View_DS(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        initView();
    }

    public Ecg12View_DS(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initView();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public Ecg12View_DS(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        this.context = context;
        initView();
    }

    private void initView() {
        labels = getResources().getStringArray(R.array.array_ecg_lead_12);
        mHolder = getHolder();
        mHolder.addCallback(this);
        mHolder.setFormat(PixelFormat.TRANSLUCENT);

        setFocusable(true);
        setFocusableInTouchMode(true);
        this.setKeepScreenOn(true);


        DisplayMetrics metric = new DisplayMetrics();
        ((Activity) context).getWindowManager().getDefaultDisplay().getRealMetrics(metric);


        scale = metric.density;
        xdpi = metric.xdpi;
        ydpi = metric.ydpi;
        xPixelPerMm = xdpi / 25.4f;
        yPixelPerMm = ydpi / 25.4f;

        mXGridWidth = xPixelPerMm * 5f;
        mYGridWidth = yPixelPerMm * 5f;


        mPath = new Paint();
        mPath.setAntiAlias(true);
        mPath.setColor(mWaveColor);
        mPath.setStrokeWidth(3f);
        mPath.setAntiAlias(true);

        waveHeight = context.getResources().getDimensionPixelSize(R.dimen.wave_height);
        waveMargin = context.getResources().getDimensionPixelSize(R.dimen.wave_margin);
        waveMargin1 = context.getResources().getDimensionPixelSize(R.dimen.wave_margin1);


        xaxis = xSpeed / sampleRate * xPixelPerMm;
        range = yPixelPerMm * enhance * zoom;

        transparentPoints = (int) ((2 * xPixelPerMm) / xaxis + 0.5f);

        mPaint = new Paint();
        mPaint.setColor(mGridColor);
        mPaint.setStrokeWidth(2f);
        mPath.setAntiAlias(true);

        mTagPaint = new Paint();
        mTagPaint.setColor(mTagColor);
        mTagPaint.setStrokeWidth(2f);

        mTextPaint = new Paint();
        mTextPaint.setColor(mTagColor);
        mTextPaint.setTextSize(26f);
        mTextPaint.setAntiAlias(true);
//        mTextPaint.setStrokeWidth(2f);
//        mTextPaint.setFakeBoldText(true);

    }

    public void setWork(boolean work) {
        this.work = work;
    }

    public void switchXSpeed(float value) {
        mIsDrawing = false;
        clearEcgData();
        xaxis = value / sampleRate * xPixelPerMm;
        maxLines = (int) ((mWidth - mXGridWidth * 5) / xaxis) / 2;
        transparentPoints = (int) ((2 * xPixelPerMm) / xaxis + 0.5f);

        mIsDrawing = true;
    }

    public void switchEnhance(float value) {
        mIsDrawing = false;
        clearEcgData();
        enhance = value;
        range = yPixelPerMm * value * zoom;
        mIsDrawing = true;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        mWidth = w;
        mHeight = h;
        maxLines = (int) ((mWidth - mXGridWidth * 5) / xaxis) / 2;
        super.onSizeChanged(w, h, oldw, oldh);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        mIsDestroyed = false;
        drawBackWhenCreate();
        new Thread(() -> {
            while (!mIsDestroyed) {
                if (mIsDrawing && work) {
                    draw();
                }
            }
        }).start();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        mIsDestroyed = true;
    }

    private void draw() {
        try {
            mCanvas = mHolder.lockCanvas();
            mCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
            drawBack(mCanvas);//画背景
            drawCanvas0(mCanvas);
            drawCanvas1(mCanvas);
            drawCanvas2(mCanvas);
            drawCanvas3(mCanvas);
            drawCanvas4(mCanvas);
            drawCanvas5(mCanvas);
            drawCanvas6(mCanvas);
            drawCanvas7(mCanvas);
            drawCanvas8(mCanvas);
            drawCanvas9(mCanvas);
            drawCanvas10(mCanvas);
            drawCanvas11(mCanvas);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (mCanvas != null)
                    mHolder.unlockCanvasAndPost(mCanvas);
                mIsDrawing = false;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    public void clearData() {
        mIsDrawing = false;
        clearEcgData();
        mIsDrawing = true;
    }

    private void clearEcgData() {
        index0 = 0;
        index1 = 0;
        index2 = 0;
        index3 = 0;
        index4 = 0;
        index5 = 0;
        index6 = 0;
        index7 = 0;
        index8 = 0;
        index9 = 0;
        index10 = 0;
        index11 = 0;

        data0.clear();
        data1.clear();
        data2.clear();
        data3.clear();
        data4.clear();
        data5.clear();
        data6.clear();
        data7.clear();
        data8.clear();
        data9.clear();
        data10.clear();
        data11.clear();
    }

    public void clearWave() {
        mIsDrawing = false;
        drawBackWhenCreate();
    }

    private void drawBackWhenCreate() {
        mCanvas = mHolder.lockCanvas();
        drawBack(mCanvas);
        if (mCanvas != null) {
            mHolder.unlockCanvasAndPost(mCanvas);
        }

    }


    public float getBaseLineHeight(int lead) {
        int baseHeight = mHeight / 12;
//        if (lead == 0 || lead == 1) return baseHeight;
//        if (lead == 2 || lead == 3) return baseHeight * 3;
//        if (lead == 4 || lead == 5) return baseHeight * 5;
//        if (lead == 6 || lead == 7) return baseHeight * 7;
//        if (lead == 8 || lead == 9) return baseHeight * 9;
//        if (lead == 10 || lead == 11) return baseHeight * 11;

        if (lead == 0 || lead == 6) return baseHeight;
        if (lead == 1 || lead == 7) return baseHeight * 3;
        if (lead == 2 || lead == 8) return baseHeight * 5;
        if (lead == 3 || lead == 9) return baseHeight * 7;
        if (lead == 4 || lead == 10) return baseHeight * 9;
        if (lead == 5 || lead == 11) return baseHeight * 11;

        return 0f;
    }

    public float getBaseLineLineWidth(int lead) {
//        if (lead % 2 == 0)
//            return 0;

        if (lead <= 5)
            return 0;

        return mWidth / 2;
    }

    private float getBaseDividerWidth(int lead) {
        if (lead <= 5) return mWidth / 2;
        return mWidth;
    }

    //划区域分割线
    private void drawDivider(Canvas canvas) {
        mTextPaint.setStrokeWidth(3f);
        canvas.drawLine(0, mHeight / 6, mWidth, mHeight / 6, mTextPaint);
        canvas.drawLine(0, mHeight / 3, mWidth, mHeight / 3, mTextPaint);
        canvas.drawLine(0, mHeight / 2, mWidth, mHeight / 2, mTextPaint);
        canvas.drawLine(0, mHeight * 2 / 3, mWidth, mHeight * 2 / 3, mTextPaint);
        canvas.drawLine(0, mHeight * 5 / 6, mWidth, mHeight * 5 / 6, mTextPaint);
        canvas.drawLine(mWidth / 2, 0, mWidth / 2, mHeight, mTextPaint);
    }

    //划导联标记
    private void drawTags(Canvas canvas) {
        float marginLeft = 5;
        float base = 1;
        //绘制导联信息
        mTextPaint.setStrokeWidth(2f);
        float mHeight = mYGridWidth * 2;

        for (int i = 0; i < labels.length; i++) {
            String label = labels[i];
            float height = getBaseLineHeight(i);
            float width = getBaseLineLineWidth(i);

            canvas.drawLine(marginLeft + width, base + height, 5 + marginLeft + width, base + height, mTagPaint);
            canvas.drawLine(5 + marginLeft + width, base + height, 5 + marginLeft + width, base + height - mHeight, mTagPaint);
            canvas.drawLine(5 + marginLeft + width, base + height - mHeight, 15 + marginLeft + width, base + height - mHeight, mTagPaint);
            canvas.drawLine(15 + marginLeft + width, base + height - mHeight, 15 + marginLeft + width, base + height, mTagPaint);
            canvas.drawLine(15 + marginLeft + width, base + height, 20 + marginLeft + width, base + height, mTagPaint);


            canvas.drawText(label, 20 + marginLeft + width + 5, base + height, mTextPaint);
        }


        //右侧竖线
//        mTextPaint.setStrokeWidth(6f);
//        for (int i = 0; i < labels.length; i++) {
//            float height = getBaseLineHeight(i);
//            float mWidth = getBaseDividerWidth(i);
//
//            canvas.drawLine(mWidth - mXGridWidth * 4, height - mYGridWidth / 2, mWidth - mXGridWidth * 4,
//                    height + mYGridWidth / 2, mTextPaint);
//        }
    }


    private void drawBack(Canvas canvas) {
        if (canvas == null)
            return;

        canvas.drawColor(mBackgroundColor);
//        //画小网格
        //竖线个数
        int vSNum = mWidth / mSGridWidth;

        //横线个数
        int hSNum = mHeight / mSGridWidth;
        mPaint.setColor(mSGridColor);
        mPaint.setStrokeWidth(2);
        //画竖线
        for (int i = 0; i < vSNum + 1; i++) {
            canvas.drawLine(i * mSGridWidth, 0, i * mSGridWidth, mHeight, mPaint);
        }
        //画横线
        for (int i = 0; i < hSNum + 1; i++) {
            canvas.drawLine(0, i * mSGridWidth, mWidth, i * mSGridWidth, mPaint);
        }
        //竖线个数
        int vNum = (int) (mWidth / mXGridWidth + 0.5f);
        //横线个数
        int hNum = (int) (mHeight / mYGridWidth + 0.5f);
        //画横线
        for (int i = 0; i < hNum + 1; i++) {
            mPaint.setColor(mGridColor);
            mPaint.setAntiAlias(true);
            canvas.drawLine(0, i * mYGridWidth, mWidth, i * mYGridWidth, mPaint);
        }

        //画竖线
        mPaint.setColor(mGridColor);
        for (int i = 0; i < vNum + 1; i++) {
            canvas.drawLine(i * mXGridWidth, 0, i * mXGridWidth, mHeight, mPaint);
        }

//        drawDivider(canvas);

        drawTags(canvas);

    }

    private void drawCanvas0(Canvas canvas) {
        for (int i = 1; i < data0.size(); i++) {
            float startX = (i - 1) * xaxis + mXGridWidth * 2;
            float stopX = i * xaxis + mXGridWidth * 2;
            if (data0.size() >= maxLines) {
                //透明点个数
                if (i <= index0 && i >= index0 - transparentPoints) {
                    mPath.setColor(Color.TRANSPARENT);
                } else {
                    mPaint.setStrokeWidth(3f);
                    mPath.setColor(mWaveColor);
                }
            }
            try {
                canvas.drawLine(startX, data0.get(i - 1), stopX, data0.get(i), mPath);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private void drawCanvas1(Canvas canvas) {
        for (int i = 1; i < data1.size(); i++) {
            float startX = (i - 1) * xaxis + mXGridWidth * 2;
            float stopX = i * xaxis + mXGridWidth * 2;
            if (data1.size() >= maxLines) {
                //透明点个数
                if (i <= index1 && i >= index1 - transparentPoints) {
                    mPath.setColor(Color.TRANSPARENT);
                } else {
                    mPaint.setStrokeWidth(3f);
                    mPath.setColor(mWaveColor);
                }
            }
            try {
                canvas.drawLine(startX, data1.get(i - 1), stopX, data1.get(i), mPath);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }

    private void drawCanvas2(Canvas canvas) {
        for (int i = 1; i < data2.size(); i++) {
            float startX = (i - 1) * xaxis + mXGridWidth * 2;
            float stopX = i * xaxis + mXGridWidth * 2;
            if (data2.size() >= maxLines) {
                //透明点个数
                if (i <= index2 && i >= index2 - transparentPoints) {
                    mPath.setColor(Color.TRANSPARENT);
                } else {
                    mPaint.setStrokeWidth(3f);
                    mPath.setColor(mWaveColor);
                }
            }
            try {
                canvas.drawLine(startX, data2.get(i - 1), stopX, data2.get(i), mPath);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private void drawCanvas3(Canvas canvas) {
        for (int i = 1; i < data3.size(); i++) {
            float startX = (i - 1) * xaxis + mXGridWidth * 2;
            float stopX = i * xaxis + mXGridWidth * 2;
            if (data3.size() >= maxLines) {
                //透明点个数
                if (i <= index3 && i >= index3 - transparentPoints) {
                    mPath.setColor(Color.TRANSPARENT);
                } else {
                    mPaint.setStrokeWidth(3f);
                    mPath.setColor(mWaveColor);
                }
            }
            try {
                canvas.drawLine(startX, data3.get(i - 1), stopX, data3.get(i), mPath);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private void drawCanvas4(Canvas canvas) {
        for (int i = 1; i < data4.size(); i++) {
            float startX = (i - 1) * xaxis + mXGridWidth * 2;
            float stopX = i * xaxis + mXGridWidth * 2;
            if (data4.size() >= maxLines) {
                //透明点个数
                if (i <= index4 && i >= index4 - transparentPoints) {
                    mPath.setColor(Color.TRANSPARENT);
                } else {
                    mPaint.setStrokeWidth(3f);
                    mPath.setColor(mWaveColor);
                }
            }
            try {
                canvas.drawLine(startX, data4.get(i - 1), stopX, data4.get(i), mPath);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private void drawCanvas5(Canvas canvas) {
        for (int i = 1; i < data5.size(); i++) {
            float startX = (i - 1) * xaxis + mXGridWidth * 2;
            float stopX = i * xaxis + mXGridWidth * 2;
            if (data5.size() >= maxLines) {
                //透明点个数
                if (i <= index5 && i >= index5 - transparentPoints) {
                    mPath.setColor(Color.TRANSPARENT);
                } else {
                    mPaint.setStrokeWidth(3f);
                    mPath.setColor(mWaveColor);
                }
            }
            try {
                canvas.drawLine(startX, data5.get(i - 1), stopX, data5.get(i), mPath);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private void drawCanvas6(Canvas canvas) {
        for (int i = 1; i < data6.size(); i++) {
            float startX = (i - 1) * xaxis + mXGridWidth * 2 + mWidth / 2;
            float stopX = i * xaxis + mXGridWidth * 2 + mWidth / 2;
            if (data6.size() >= maxLines) {
                //透明点个数
                if (i <= index6 && i >= index6 - transparentPoints) {
                    mPath.setColor(Color.TRANSPARENT);
                } else {
                    mPaint.setStrokeWidth(3f);
                    mPath.setColor(mWaveColor);
                }
            }
            try {
                canvas.drawLine(startX, data6.get(i - 1), stopX, data6.get(i), mPath);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private void drawCanvas7(Canvas canvas) {
        for (int i = 1; i < data7.size(); i++) {
            float startX = (i - 1) * xaxis + mWidth / 2 + mXGridWidth * 2;
            float stopX = i * xaxis + mWidth / 2 + mXGridWidth * 2;
            if (data7.size() >= maxLines) {
                //透明点个数
                if (i <= index7 && i >= index7 - transparentPoints) {
                    mPath.setColor(Color.TRANSPARENT);
                } else {
                    mPaint.setStrokeWidth(3f);
                    mPath.setColor(mWaveColor);
                }
            }
            try {
                canvas.drawLine(startX, data7.get(i - 1), stopX, data7.get(i), mPath);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private void drawCanvas8(Canvas canvas) {
        for (int i = 1; i < data8.size(); i++) {
            float startX = (i - 1) * xaxis + mXGridWidth * 2 + mWidth / 2;
            float stopX = i * xaxis + mXGridWidth * 2 + mWidth / 2;
            if (data8.size() >= maxLines) {
                //透明点个数
                if (i <= index8 && i >= index8 - transparentPoints) {
                    mPath.setColor(Color.TRANSPARENT);
                } else {
                    mPaint.setStrokeWidth(3f);
                    mPath.setColor(mWaveColor);
                }
            }
            try {
                canvas.drawLine(startX, data8.get(i - 1), stopX, data8.get(i), mPath);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private void drawCanvas9(Canvas canvas) {
        for (int i = 1; i < data9.size(); i++) {
            float startX = (i - 1) * xaxis + mXGridWidth * 2 + mWidth / 2;
            float stopX = i * xaxis + mXGridWidth * 2 + mWidth / 2;
            if (data9.size() >= maxLines) {
                //透明点个数
                if (i <= index9 && i >= index9 - transparentPoints) {
                    mPath.setColor(Color.TRANSPARENT);
                } else {
                    mPaint.setStrokeWidth(3f);
                    mPath.setColor(mWaveColor);
                }
            }
            try {
                canvas.drawLine(startX, data9.get(i - 1), stopX, data9.get(i), mPath);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private void drawCanvas10(Canvas canvas) {
        for (int i = 1; i < data10.size(); i++) {
            float startX = (i - 1) * xaxis + mXGridWidth * 2 + mWidth / 2;
            float stopX = i * xaxis + mXGridWidth * 2 + mWidth / 2;
            if (data10.size() >= maxLines) {
                //透明点个数
                if (i <= index10 && i >= index10 - transparentPoints) {
                    mPath.setColor(Color.TRANSPARENT);
                } else {
                    mPaint.setStrokeWidth(3f);
                    mPath.setColor(mWaveColor);
                }
            }
            try {
                canvas.drawLine(startX, data10.get(i - 1), stopX, data10.get(i), mPath);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private void drawCanvas11(Canvas canvas) {
        for (int i = 1; i < data11.size(); i++) {
            float startX = (i - 1) * xaxis + mXGridWidth * 2 + mWidth / 2;
            float stopX = i * xaxis + mXGridWidth * 2 + mWidth / 2;
            if (data11.size() >= maxLines) {
                //透明点个数
                if (i <= index11 && i >= index11 - transparentPoints) {
                    mPath.setColor(Color.TRANSPARENT);
                } else {
                    mPaint.setStrokeWidth(3f);
                    mPath.setColor(mWaveColor);
                }
            }
            try {
                canvas.drawLine(startX, data11.get(i - 1), stopX, data11.get(i), mPath);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void notifyData0(ArrayList<EcgWaveData> data) {
        int tempType = -1;
        int tempIndex = index0;
        for (EcgWaveData bean : data) {
            if (tempType == -1) {
                tempType = bean.type;
            }
            int key = bean.type;
            float value = -((bean.value) * yPixelPerMm * enhance * zoom);

            if (tempType != key) {
                index0 = tempIndex;
                tempType = key;
            }

            if (data0.size() >= maxLines) {
                data0.remove(index0);
                data0.add(index0, getBaseLineHeight(key) + value);
            } else {
                data0.add(getBaseLineHeight(key) + value);
            }
            index0++;
            if (index0 >= maxLines) {
                index0 = 0;
            }
        }
        mIsDrawing = true;
    }

    public void notifyData1(ArrayList<EcgWaveData> data) {
        int tempType = -1;
        int tempIndex = index1;
        for (EcgWaveData bean : data) {
            if (tempType == -1) {
                tempType = bean.type;
            }
            int key = bean.type;
            float value = -((bean.value) * yPixelPerMm * enhance * zoom);

            if (tempType != key) {
                index1 = tempIndex;
                tempType = key;
            }

            if (data1.size() >= maxLines) {
                data1.remove(index1);
                data1.add(index1, getBaseLineHeight(key) + value);
            } else {
                data1.add(getBaseLineHeight(key) + value);
            }
            index1++;
            if (index1 >= maxLines) {
                index1 = 0;
            }
        }
        mIsDrawing = true;
    }

    public void notifyData2(ArrayList<EcgWaveData> data) {
        int tempType = -1;
        int tempIndex = index2;
        for (EcgWaveData bean : data) {
            if (tempType == -1) {
                tempType = bean.type;
            }
            int key = bean.type;
            float value = -((bean.value) * yPixelPerMm * enhance * zoom);

            if (tempType != key) {
                index2 = tempIndex;
                tempType = key;
            }
            if (data2.size() >= maxLines) {
                data2.remove(index2);
                data2.add(index2, getBaseLineHeight(key) + value);
            } else {
                data2.add(getBaseLineHeight(key) + value);
            }
            index2++;
            if (index2 >= maxLines) {
                index2 = 0;
            }
        }
        mIsDrawing = true;
    }

    public void notifyData3(ArrayList<EcgWaveData> data) {
        int tempType = -1;
        int tempIndex = index3;
        for (EcgWaveData bean : data) {
            if (tempType == -1) {
                tempType = bean.type;
            }
            int key = bean.type;
            float value = -((bean.value) * yPixelPerMm * enhance * zoom);

            if (tempType != key) {
                index3 = tempIndex;
                tempType = key;
            }

            if (data3.size() >= maxLines) {
                data3.remove(index3);
                data3.add(index3, getBaseLineHeight(key) + value);
            } else {
                data3.add(getBaseLineHeight(key) + value);
            }
            index3++;
            if (index3 >= maxLines) {
                index3 = 0;
            }
        }
        mIsDrawing = true;
    }

    public void notifyData4(ArrayList<EcgWaveData> data) {
        int tempType = -1;
        int tempIndex = index4;
        for (EcgWaveData bean : data) {
            if (tempType == -1) {
                tempType = bean.type;
            }
            int key = bean.type;
            float value = -((bean.value) * yPixelPerMm * enhance * zoom);

            if (tempType != key) {
                index4 = tempIndex;
                tempType = key;
            }

            if (data4.size() >= maxLines) {
                data4.remove(index4);
                data4.add(index4, getBaseLineHeight(key) + value);
            } else {
                data4.add(getBaseLineHeight(key) + value);
            }
            index4++;
            if (index4 >= maxLines) {
                index4 = 0;
            }
        }
        mIsDrawing = true;
    }

    public void notifyData5(ArrayList<EcgWaveData> data) {
        int tempType = -1;
        int tempIndex = index5;
        for (EcgWaveData bean : data) {
            if (tempType == -1) {
                tempType = bean.type;
            }
            int key = bean.type;
            float value = -((bean.value) * yPixelPerMm * enhance * zoom);

            if (tempType != key) {
                index5 = tempIndex;
                tempType = key;
            }

            if (data5.size() >= maxLines) {
                data5.remove(index5);
                data5.add(index5, getBaseLineHeight(key) + value);
            } else {
                data5.add(getBaseLineHeight(key) + value);
            }
            index5++;
            if (index5 >= maxLines) {
                index5 = 0;
            }
        }
        mIsDrawing = true;
    }

    public void notifyData6(ArrayList<EcgWaveData> data) {
        int tempType = -1;
        int tempIndex = index6;
        for (EcgWaveData bean : data) {
            if (tempType == -1) {
                tempType = bean.type;
            }
            int key = bean.type;
            float value = -((bean.value) * yPixelPerMm * enhance * zoom);

            if (tempType != key) {
                index6 = tempIndex;
                tempType = key;
            }

            if (data6.size() >= maxLines) {
                data6.remove(index6);
                data6.add(index6, getBaseLineHeight(key) + value);
            } else {
                data6.add(getBaseLineHeight(key) + value);
            }
            index6++;
            if (index6 >= maxLines) {
                index6 = 0;
            }
        }
        mIsDrawing = true;
    }

    public void notifyData7(ArrayList<EcgWaveData> data) {
        int tempType = -1;
        int tempIndex = index7;
        for (EcgWaveData bean : data) {
            if (tempType == -1) {
                tempType = bean.type;
            }
            int key = bean.type;
            float value = -((bean.value) * yPixelPerMm * enhance * zoom);

            if (tempType != key) {
                index7 = tempIndex;
                tempType = key;
            }

            if (data7.size() >= maxLines) {
                data7.remove(index7);
                data7.add(index7, getBaseLineHeight(key) + value);
            } else {
                data7.add(getBaseLineHeight(key) + value);
            }
            index7++;
            if (index7 >= maxLines) {
                index7 = 0;
            }
        }
        mIsDrawing = true;
    }

    public void notifyData8(ArrayList<EcgWaveData> data) {
        int tempType = -1;
        int tempIndex = index8;
        for (EcgWaveData bean : data) {
            if (tempType == -1) {
                tempType = bean.type;
            }
            int key = bean.type;
            float value = -((bean.value) * yPixelPerMm * enhance * zoom);

            if (tempType != key) {
                index8 = tempIndex;
                tempType = key;
            }

            if (data8.size() >= maxLines) {
                data8.remove(index8);
                data8.add(index8, getBaseLineHeight(key) + value);
            } else {
                data8.add(getBaseLineHeight(key) + value);
            }
            index8++;
            if (index8 >= maxLines) {
                index8 = 0;
            }
        }
        mIsDrawing = true;
    }

    public void notifyData9(ArrayList<EcgWaveData> data) {
        int tempType = -1;
        int tempIndex = index9;
        for (EcgWaveData bean : data) {
            if (tempType == -1) {
                tempType = bean.type;
            }
            int key = bean.type;
            float value = -((bean.value) * yPixelPerMm * enhance * zoom);

            if (tempType != key) {
                index9 = tempIndex;
                tempType = key;
            }

            if (data9.size() >= maxLines) {
                data9.remove(index9);
                data9.add(index9, getBaseLineHeight(key) + value);
            } else {
                data9.add(getBaseLineHeight(key) + value);
            }
            index9++;
            if (index9 >= maxLines) {
                index9 = 0;
            }
        }
        mIsDrawing = true;
    }

    public void notifyData10(ArrayList<EcgWaveData> data) {
        int tempType = -1;
        int tempIndex = index10;
        for (EcgWaveData bean : data) {
            if (tempType == -1) {
                tempType = bean.type;
            }
            int key = bean.type;
            float value = -((bean.value) * yPixelPerMm * enhance * zoom);

            if (tempType != key) {
                index10 = tempIndex;
                tempType = key;
            }

            if (data10.size() >= maxLines) {
                data10.remove(index10);
                data10.add(index10, getBaseLineHeight(key) + value);
            } else {
                data10.add(getBaseLineHeight(key) + value);
            }
            index10++;
            if (index10 >= maxLines) {
                index10 = 0;
            }
        }
        mIsDrawing = true;
    }

    public void notifyData11(ArrayList<EcgWaveData> data) {
        int tempType = -1;
        int tempIndex = index11;
        for (EcgWaveData bean : data) {
            if (tempType == -1) {
                tempType = bean.type;
            }
            int key = bean.type;
            float value = -((bean.value) * yPixelPerMm * enhance * zoom);

            if (tempType != key) {
                index11 = tempIndex;
                tempType = key;
            }
            if (data11.size() >= maxLines) {
                data11.remove(index11);
                data11.add(index11, getBaseLineHeight(key) + value);
            } else {
                data11.add(getBaseLineHeight(key) + value);
            }
            index11++;
            if (index11 >= maxLines) {
                index11 = 0;
            }
        }
        mIsDrawing = true;
    }


}
